package com.powerhua.core.web;

import com.powerhua.core.utils.WebPathUtils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Administrator
 */
public class HtmlContentAction implements IServletAction {

    @Override
    public String doAction(HttpServletRequest request) {
        String url = request.getParameter("file_path");
        String basePath = WebPathUtils.getWebContentPath();
        String result = getPageContent(basePath + "/" + url);
        return result;
    }

    private String getPageContent(String filePath) {
        StringBuffer result = new StringBuffer("");

        String content = getTextFileStr(filePath, "UTF-8");
        result.append(content);

        return result.toString();
    }

    private String getTextFileStr(String filePath, String coding) {
        StringBuffer sb = new StringBuffer();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), coding));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sb.append(tempStr);
                sb.append(System.getProperty("line.separator"));
            }
            reader.close();
        } catch (Exception e) {
            // TODO 
            e.printStackTrace();
        }
        return sb.toString();
    }
}